package com.hang.mapstruct;


import com.hang.entity.User;
import com.hang.model.dto.request.UserInfoRegisterDTO;
import com.hang.model.dto.response.UserInfoModel;
import com.hang.model.vo.UserInfoDetailVO;
import com.hang.model.vo.UserInfoVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.LinkedList;
import java.util.List;

/**
 * @author lyh
 * @date 2022/11/22
 */

@Mapper(componentModel = "spring")
public interface UserMapStruct {
    UserInfoModel userToUserInfoModel(User user);

    User userInfoRegisterDTOTOUser(UserInfoRegisterDTO userInfoRegisterDTO);

    default List<UserInfoVO> usersToUserInfoVOList(List<User> users){
        List<UserInfoVO> userInfoVOList = new LinkedList<>();
        users.forEach(
                user -> {
                    UserInfoVO userInfoVO = userToUserInfoVO(user);
                    userInfoVOList.add(userInfoVO);
                }
        );

        return userInfoVOList;
    }

    @Mapping(source = "gender",target = "gender",qualifiedByName = "genderFormatter")
    UserInfoVO userToUserInfoVO(User user);

    @Mapping(source = "gender",target = "gender",qualifiedByName = "genderFormatter")
    UserInfoDetailVO userToUserInfoDetailVO(User user);

    @Named("genderFormatter")
    default String genderFormatter(Integer gender){
        return gender == 0?"女":"男";
    }
}